package com.pojos.prg;


import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

public class PersonTest {
	
	public static void getPersonDetails(int id,Session ses){
		
		Person p=(Person) ses.get(Person.class, id);
		PersonDetail pd=p.getpDetail();
		System.out.println("PersonDetails Of:"+id+", is");
		System.out.println(p);
		
	}
	
	public static void getZipCode(int zipcode,Session ses){
		String str= " FROM PersonDetail p where p.zipCode=:zip";
		Query q=ses.createQuery(str);
		//q.setParameter(1,zipcode);
		q.setParameter("zip", zipcode);
		List<PersonDetail> lp=q.list();
		
		//PersonDetail pd=(PersonDetail) ses.load(PersonDetail.class, zipcode);
		for(PersonDetail pd:lp)
		System.out.println(pd);
		
		
		
	}
	
	public static void main(String[] args) throws IllegalStateException {
		
		@SuppressWarnings("deprecation")
		AnnotationConfiguration acfg=new AnnotationConfiguration();
		//acfg.addClass(persistentClass)
		acfg.configure();
		SessionFactory sf=acfg.buildSessionFactory();
		Session ses=sf.openSession();
				
			Person p=new Person();
			p.setName("Joshna");
			//p.setpId(1);
			
			PersonDetail pd=new PersonDetail();
			pd.setJob("java");
			pd.setLoc("Hyd");
			pd.setPhNo(8600);
			pd.setSalary(35000);
			pd.setZipCode(518413);
			
			p.setpDetail(pd);
		
			Transaction tx=(Transaction) ses.beginTransaction();
			
					//int i=(int) ses.save(p);
					//ses.save(pd);
					System.out.println("printing the details Of a Particular Person: ");
					//getPersonDetails(20, ses);
					System.out.println("getting the details by using Zip code:");
					getZipCode(518412, ses);
					try {
						tx.commit();
					} catch (Exception e){
						System.out.println("Exception caused.------------------------");
							e.printStackTrace();
								tx.rollback();
					}
			
		
	}

}
